package com.rui.cn.zull.fallback;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.springframework.cloud.netflix.zuul.filters.route.ZuulFallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

/**   
 * @ClassName:  ProjectFallback   
 * @Description:zuul的失败回退配置 
 * @author: 【zhang】
 * @date:   2018年5月15日 下午20:54:37
 */
@Component
public class ProjectFallback implements ZuulFallbackProvider{

	@Override
	public ClientHttpResponse fallbackResponse() {
		return new ClientHttpResponse() {
			
			@Override
			public HttpHeaders getHeaders() {
				HttpHeaders headers=new HttpHeaders();
				headers.set("Content-Type","text/html; charset=UTF-8");
				return headers;
			}
			
			@Override
			public InputStream getBody() throws IOException {
				// 当出现服务调用错误之后返回的数据内容
				return new ByteArrayInputStream(
						"{\"status\":400,\"message\":\"【ERROR】Zuul-Fallback,Gateway客户端提供\"}".getBytes());
			}
			
			@Override
			public String getStatusText() throws IOException {
				return HttpStatus.BAD_REQUEST.getReasonPhrase();
			}
			
			@Override
			public HttpStatus getStatusCode() throws IOException {
				return HttpStatus.BAD_REQUEST;
			}
			
			@Override
			public int getRawStatusCode() throws IOException {
				return HttpStatus.BAD_REQUEST.value();
			}
			
			@Override
			public void close() {
				
			}
		};
	}

	  /**   
	 * <p>Title: getRoute</p>   
	 * <p>Description:设置失败回退的路由 </p>   
	 * @return   
	 * @see ZuulFallbackProvider#getRoute()
	 */
	@Override
	public String getRoute() {
		return "permisssion-provider";
	}

}
